<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Interfaces\OpenWorkApplicationInterface;
use App\CustHub\Request\OpenWorkRequest;
use App\CustHub\Work\OpenWorkAgentService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Mine\MineController;
use function Hyperf\Collection\data_get;

#[Controller(prefix: 'ch/openwork/auth'),ApiAuth]
class OpenWorkAuthController extends MineController
{
    #[Inject]
    protected OpenWorkAgentService $workAgentService;

    #[Inject]
    protected OpenWorkApplicationInterface $openWorkApplication;

    /**
     * 获取授权二维码
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[RequestMapping('getQrCode')]
    public function getQrCode(): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->request->all();
        $authType = data_get($data,'auth_type',0);
        $mainUid = $data['main_uid'];
        //获取授权二维码
        $authUrl = $this->openWorkApplication->getAuthQrCode((int)$authType,$mainUid);
        return $this->success(['url'=>$authUrl]);
    }

    /**
     * 检查授权状态
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[RequestMapping('checkAuthStatus')]
    public function checkAuthStatus(OpenWorkRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        $authType = data_get($data,'auth_type',0);
        return $this->success(['status'=>$this->workAgentService->checkAuthStatus($data['corp_wxid'],(int)$authType)]);
    }
}